package com.qingmi.farmer.ui.demand;

import android.content.Context;
import android.content.Intent;

import com.qingmi.farmer.BR;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.ActionsCode;
import com.qingmi.farmer.data.UserData;
import com.qingmi.farmer.data.model.GrainDemand;
import com.qingmi.farmer.data.repository.data.GrainDataRepository;
import com.qingmi.farmer.databinding.ActivityDemandDetailBinding;
import com.wjjun.base.ui.base.ListActivity;
import com.qingmi.farmer.viewmodel.GrainDemandFooterViewModel;
import com.qingmi.farmer.viewmodel.GrainDemandHeaderViewModel;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.data.request.SimpleRequest;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;


/**
 * 收购详情
 */
public class DemandDetailActivity extends ListActivity<DemandDetailViewModel, ActivityDemandDetailBinding> {

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, DemandDetailActivity.class);
        intent.putExtra(ActionsCode.DATA_ID, id);
        context.startActivity(intent);
    }

    private GrainDemandHeaderViewModel headerViewModel;
    private GrainDemandFooterViewModel footerViewModel;

    @Inject
    GrainDataRepository grainDataRepository;


    @Inject
    UserData userData;

    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_demand_detail;
    }

    @Override
    protected DemandDetailViewModel initViewModel() {
        return new DemandDetailViewModel();
    }

    @Override
    protected void initContent() {
        super.initContent();
        grainDataRepository.bind(this);

        viewModel.setTitle(getString(R.string.title_demand_detail));

        commListViewModel.setEnableLoadMore(false);
        commListViewModel.setEnableRefresh(true);

        headerViewModel = new GrainDemandHeaderViewModel();
        footerViewModel = new GrainDemandFooterViewModel();

        id = getIntent().getStringExtra(ActionsCode.DATA_ID);
    }

    @Override
    public void onListItemBind(ItemBinding itemBinding, int position, Object item) {
        if (item instanceof GrainDemandHeaderViewModel) {
            itemBinding.set(BR.viewModel, R.layout.layout_grain_demand_detail_header).bindExtra(BR.presenter, viewGroupPresenter);
        } else if (item instanceof GrainDemandFooterViewModel) {
            itemBinding.set(BR.viewModel, R.layout.layout_grain_demand_detail_footer).bindExtra(BR.presenter, viewGroupPresenter);
        }
    }

    @Override
    public void refreshData(boolean init) {
        super.refreshData(init);
        grainDataRepository.getGrainDemandDetail(new DefaultObserver<GrainDemand>() {

            @SuppressWarnings("unchecked")
            @Override
            public void onNext(GrainDemand grainDemand) {
                if (grainDemand == null) {
                    showToast(R.string.error_demand_detail_is_error);
                    finish();
                    return;
                }
                data.clear();
                headerViewModel.bindGrainDemand(grainDemand);

                data.add(headerViewModel);
//
//                if (grainDemand.getAddresses() != null && !grainDemand.getAddresses().isEmpty()) {
//                    for (Address item : grainDemand.getAddresses()) {
//                        data.add(new SelectAddressItemViewModel(item, DataSetting.DELIVERY_TYPE_SELF));
//                    }
//                }

                data.add(footerViewModel);

                finisRefresh();
            }

            @Override
            public void onError(MyException e) {
                setError(e.getMessage());
            }
        }, new SimpleRequest(id));
    }

}
